import 'package:flutter/material.dart';
import 'package:flutter_base_library/init/init_app.dart';
import 'package:get/get.dart';

class BaseTabBarController extends GetxController {
  BaseTabBarController();

  /// 响应式成员变量，默认位置指引0
  final _currentPage = 0.obs;
  set currentPage(index) => _currentPage.value = index;
  get currentPage => _currentPage.value;

  /// PageView页面控制器
  late PageController pageController;

  /// Page页面集合
  final tabPageBodies = <Widget>[].obs;

  /// 底部BottomNavigationBarItem
  final bottomTabs = <BottomNavigationBarItem>[].obs;

  switchBottomTabBar(int index) {
    //点击底部BottomNavigationBarItem切换PageView页面
    //pageController.animateToPage(index,duration: Duration(seconds: 1),curve: Curves.fastLinearToSlowEaseIn);
    pageController.jumpToPage(index);
  }

  onPageChanged(int index) {
    currentPage = index;
  }

  /// 在Widget内存中分配后立即调用，可以用它来初始化initialize一些东西
  @override
  void onInit() {
    super.onInit();
    pageController = PageController(initialPage: currentPage);

    if (InitApp.instance.initTabbarItem != null) {
      var item = InitApp.instance.initTabbarItem!();
      bottomTabs.value = List.generate(item.length, (index) {
        return item[index].barItem;
      });
      tabPageBodies.value = List.generate(item.length, (index) {
        return item[index].page;
      });
    }
  }
}

class BaseTabbarItem {
  final BottomNavigationBarItem barItem;
  final Widget page;
  BaseTabbarItem(this.barItem, this.page);
}
